import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;

public class test {
    public static void main(String[] args) {
       // fileWriter();
        // 替换为您要检查的文件路径
        String filePath = "D:\\dhzn\\ouman\\24030027-特定4.85米高柜-4850x400x3070-香榭丽橡木-5001240304001-.xls";

//        File file = new File(filePath);

        try (FileInputStream fileInputStream = new FileInputStream((filePath));
             HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream)) {

            Sheet sheet = workbook.getSheetAt(0);

            for (int i = sheet.getNumMergedRegions() -1 ; i >= 0; i--) {
                // 合并单元格位置
                CellRangeAddress range = sheet.getMergedRegion(i);
                // 拆分3行与4行的单元格
                if (range.getFirstRow() >= 5) {
                    // 拆分单元格
                    sheet.removeMergedRegion(i);
                    int firstColumn = range.getFirstColumn();
                    int firstRow = range.getFirstRow();
                    int lastRow = range.getLastRow();
                    int LastColumn = range.getLastColumn();
                    Cell cfirst = sheet.getRow(firstRow).getCell(firstColumn);
                    // 填充被拆分单元格的值
                    for (int m = firstRow; m <= lastRow; m++) {
                        for (int n = firstColumn; n <= LastColumn; n++) {
                            Cell tc = sheet.getRow(m).getCell(n);
                            tc.setCellStyle(cfirst.getCellStyle());
                            tc.setCellValue(cfirst.getStringCellValue());
                        }
                    }
                }
            }


        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static boolean isRowEmpty(Row row) {
        for (int c = 0; c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
            if (cell != null && cell.getCellType() != CellType.BLANK) {
                return false;
            }
        }
        return true;
    }

    private static String getCellValueAsString(Cell cell) {
        String cellValue = "";
        switch (cell.getCellType()) {
            case STRING:
                cellValue = cell.getStringCellValue();
                break;
            case NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case FORMULA:
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            default:
                cellValue = "";
        }
        return cellValue;
    }

    private static void fileWriter(){

            try {
                String filePath = "D:\\oumans\\filename.txt";
                BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true));
                writer.write(123);
                writer.newLine();
                writer.write(456);
                writer.newLine();
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}